<?php

namespace Common\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Base extends Model
{
    use SoftDeletes;

    /**
     * 查询一条数据(通过主键id查询)
     * @param $query
     * @param int $id
     * @param array $arr_option
     * @return array
     */
    public function scopeCommonFind($query, int $id, $arr_option = [])
    {
        $arr_option = self::configOption($arr_option);
        $info = $query->with($arr_option['with'])
            ->select($arr_option['field'])
            ->withCount($arr_option['withCount'])//必须放在select后面才会生效
            ->find($id);

        return empty($info) ? [] : $info->toArray();
    }

    /**
     * 查询一条数据(通过where条件查询)
     * @param $query
     * @param array $arr_option
     * @return array
     */
    public function scopeCommonGetOne($query, $arr_option = [])
    {
        $arr_option = self::configOption($arr_option);
        $info = $query->with($arr_option['with'])
            ->where($arr_option['where'])
            ->orWhere($arr_option['orWhere'])
            ->select($arr_option['field'])
            ->withCount($arr_option['withCount'])
            ->get()
            ->toArray();

        return empty($info) ? [] : $info[0];
    }

    /**
     * 获取所有数据
     * @param $query
     * @param array $arr_option
     * @return mixed
     */
    public function scopeCommonGetAll($query, $arr_option = [])
    {
        $arr_option = self::configOption($arr_option);
        return $query->with($arr_option['with'])
            ->where($arr_option['where'])
            ->orWhere(function ($query) use ($arr_option) {
                if (!empty($arr_option['orWhere'])) {
                    $query->where($arr_option['orWhere']);
                }
            })
            ->select($arr_option['field'])
            ->withCount($arr_option['withCount'])
            ->orderBy($arr_option['order']['key'], $arr_option['order']['value'])
            ->get()
            ->toArray();
    }

    /**
     * 获取列表数据-含分页
     * @param $query
     * @param array $arr_option
     * @return mixed
     */
    public function scopeCommonGetList($query, $arr_option = [])
    {
        $arr_option = self::configOption($arr_option);
        return $query->with($arr_option['with'])
            ->where($arr_option['where'])
            ->where(function ($query) use ($arr_option) {
                if (!empty($arr_option['like'])) {
                    $query->orWhere($arr_option['like']['key'], 'like', '%' . $arr_option['like']['value'] . '%');
                }
            })
            ->orWhere(function ($query) use ($arr_option) {
                if (!empty($arr_option['orWhere'])) {
                    $query->where($arr_option['orWhere']);
                }
            })
            ->select($arr_option['field'])
            ->withCount($arr_option['withCount'])
            ->orderBy($arr_option['order']['key'], $arr_option['order']['value'])
            ->paginate($arr_option['limit'])
            ->toArray();
    }

    /**
     * 删除数据
     * @param $query
     * @param bool $bool
     * @return mixed
     */
    public function scopeCommonDelete($query, bool $bool = false)
    {
        if ($bool === true) {
            return $query->forceDelete();  //物理删除
        } else {
            return $query->delete();       //软删除
        }
    }

    /**
     * 基础config
     * @param $arr_option
     * @return mixed
     */
    public function configOption($arr_option)
    {
        $arr_common_config = self::commonConfig();
        if (empty($arr_option['with'])) {
            $arr_option['with'] = $arr_common_config['with'];
        }
        if (empty($arr_option['where'])) {
            $arr_option['where'] = $arr_common_config['where'];
        }
        if (empty($arr_option['orWhere'])) {
            $arr_option['orWhere'] = $arr_common_config['orWhere'];
        }
        if (empty($arr_option['field'])) {
            $arr_option['field'] = $arr_common_config['field'];
        }
        if (empty($arr_option['order'])) {
            $arr_option['order'] = $arr_common_config['order'];
        }
        if (empty($arr_option['limit'])) {
            $arr_option['limit'] = $arr_common_config['limit'];
        }
        if (empty($arr_option['withCount'])) {
            $arr_option['withCount'] = $arr_common_config['withCount'];
        }

        return $arr_option;
    }

    /**
     * 获取公共配置
     * @return array
     */
    public function commonConfig()
    {
        $arr_data = [
            'with' => [],//默认关联
            'where' => [],//默认查询条件
            'orWhere' => [],//默认查询条件
            'field' => ['*'],//默认查询字段
            'order' => ['key' => 'id', 'value' => 'desc'],//默认排序方式
            'page' => 10,//默认每页查询条数
            'withCount' => [],//默认关联统计
        ];
        return $arr_data;
    }

}
